package View;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import Controler.ConnectToServerListener;
import Controler.InformationsListener;
import Controler.LaunchGameListener;
import Controler.LaunchServerListener;
import Controler.CheckerboardListener;
import Data.GameStatus;
import Data.Grid;
import Model.Piece;
import Model.PropertiesUtils;

/**
 * Main frame application
 * @author tanguy.falconnet
 *
 */
public class Window extends JFrame implements Observer{
	private static final long serialVersionUID = 1L;
	public static final int FRAME_WIDTH = 800;
	public static final int FRAME_HEIGHT = 600;
	private Checkerboard checkerboard;
	private Informations informations;
	private JMenuBar menuBar;
	private JMenuItem itemNetworkServer;
	private JMenuItem itemNetworkConnect;
	private JMenuItem launchItem;
	
	/**
	 * Construtor
	 */
	public Window(){
		menuBar = new JMenuBar();
		JMenu gameMenu = new JMenu("Jeu");
		launchItem = new JMenuItem("Lancer");

		gameMenu.add(launchItem);
		
		JMenu menuNetwork = new JMenu("Réseau");
		itemNetworkServer = new JMenuItem("Lancer Serveur...");		
		itemNetworkConnect = new JMenuItem("Se Connecter...");
		menuNetwork.add(itemNetworkServer);
		menuNetwork.add(itemNetworkConnect);
		
		menuBar.add(gameMenu);
		menuBar.add(menuNetwork);
		
		checkerboard = new Checkerboard();
		informations = new Informations();
		this.setTitle("Chess Game");
		this.setLayout(new FlowLayout());
		this.add(checkerboard);
		this.add(informations);
		this.setJMenuBar(menuBar);
		this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true); 
		this.setResizable(false);
		this.pack();
	}
	
	/**
	 * Add listener to windows elements
	 * @param CheckerboardListener
	 * @param LaunchServerListener
	 * @param ConnectToServerListener
	 * @param LaunchGameListener
	 * @param InformationsListener
	 */
	public void addListener(CheckerboardListener l, LaunchServerListener lsl, ConnectToServerListener ctsl,
			LaunchGameListener lgl, InformationsListener il){
		checkerboard.addMouseListener(l);
		checkerboard.addMouseMotionListener(l);
		checkerboard.addKeyListener(l);
		itemNetworkServer.addActionListener(lsl);
		itemNetworkConnect.addActionListener(ctsl);
		launchItem.addActionListener(lgl);
		informations.addListenerToTextFields(il, il);
	}
	
	public void removeListener(CheckerboardListener l, InformationsListener il){
		checkerboard.removeMouseListener(l);
		checkerboard.removeMouseMotionListener(l);
		informations.removeListenerToTextFields(il, il);
	}
	
	/**
	 * Update by model (see DP Observer/Observable)
	 */
	@Override
	public void update(Observable obs, Object obj) {
		if(obj instanceof char[][]){
			checkerboard.reload((char[][]) obj);
		}else if(obj instanceof boolean[][]){
			checkerboard.help((boolean[][])obj);
		}else if(obj instanceof Grid){
			checkerboard.reload((Grid)obj);
		}else if(obj instanceof GameStatus){
			GameStatus gameStatus = (GameStatus)obj;
			if(gameStatus.isFinish()){
				this.setInformations(PropertiesUtils.informationsProperties("echec") + "\n" + PropertiesUtils.informationsProperties("win"));
			}			
			this.setScore(Integer.parseInt(PropertiesUtils.informationsProperties("player1"))
					,gameStatus.getScore(Integer.parseInt(PropertiesUtils.informationsProperties("player1"))));
			this.setScore(Integer.parseInt(PropertiesUtils.informationsProperties("player2"))
					,gameStatus.getScore(Integer.parseInt(PropertiesUtils.informationsProperties("player2"))));
		}
		this.repaint();
	}

	/**
	 * Set the message information in panel information
	 * @param String Message to display
	 */
	public void setInformations(String message){
		informations.setInformations(message);
	}
	
	/**
	 * Set the player score
	 * @param int Score to set
	 * @param int Player to set
	 */
	public void setScore(int player, int score){
		if(player == Integer.parseInt(PropertiesUtils.informationsProperties("player1"))){
			informations.setScore1(score);
		}else if(player == Integer.parseInt(PropertiesUtils.informationsProperties("player2"))){
			informations.setScore2(score);
		}
	}
	
	/**
	 * Set the player name
	 * @param int Name to set
	 * @param int Player to set
	 */
	public void setPlayerName(int player, String name){
		if(player == Integer.parseInt(PropertiesUtils.informationsProperties("player1"))){
			informations.setPlayer1Name(name);
		}else if(player == Integer.parseInt(PropertiesUtils.informationsProperties("player2"))){
			informations.setPlayer2Name(name);
		}
	}
	
	public void displayLocalPlayer(int player){
		informations.localPlayer(player);
	}
	
	
	
}
